<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="30">
        <el-col :span="10">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable size="small" @clear="getGoodList" @keyup.enter.native="getGoodList">
            <el-button slot="append" icon="el-icon-search" size="small" @click="btnSearch"></el-button>
          </el-input>
        </el-col>
        <el-col :span="14">
            <el-button type="primary" size="small" round @click="addGood">添加商品</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodsList" border stripe>
          <el-table-column type="index" label="#" align="center"></el-table-column>
          <el-table-column label="商品名称" prop="goods_name"></el-table-column>
          <el-table-column label="商品价格" prop="goods_price" align="center"></el-table-column>
          <el-table-column label="商品重量" prop="goods_weight" align="center"></el-table-column>
          <el-table-column label="创建时间" align="center">
              <template slot-scope="scoped">
                  {{scoped.row.add_time | dateFormat}}
              </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
              <template slot-scope="scoped">
                  <div>
                      <el-button type="primary" size="mini" round class="el-icon-edit"></el-button>
                      <el-button type="danger" size="mini" round class="el-icon-delete" @click="removeById(scoped.row)"></el-button>
                  </div>
              </template>
          </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
        queryInfo:{
            query: '',
            pagenum: 1,
            pagesize: 5,
        },
        goodsList:[],
        total: 0,
        //添加商品对话框
        addDialogVisiable: false,
        //添加商品表单内容
        addGoodForm:{
            goods_name: '',
            goods_cat: '',
            goods_price: '',
            goods_number: '',
            goods_weight: '',
        },
    };
  },
  created() {
      this.getGoodList();
  },
  methods: {
      async getGoodList(){
          const {data: res} = await this.$http.get('goods',{params: this.queryInfo});
          if(res.meta.status !== 200) return this.$message.error('获取商品列表失败！');
          this.goodsList = res.data.goods;
          this.total = res.data.total;
          console.log(res.data);
      },
      //调整每页显示数量
      handleSizeChange(value){
          this.queryInfo.pagesize = value;
          this.getGoodList();
      },
      //现在查看的页面事件
      handleCurrentChange(value){
          this.queryInfo.pagenum = value;
          this.getGoodList();
      },
      btnSearch(){
          this.getGoodList();
      },
      //添加商品
      addGood(){
          this.$router.push('/goods/add');
      },
      //删除商品方法
      async removeById(row){
          const result = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err);
        if(result !== 'confirm') return this.$message.info('已取消删除');
        console.log(row);
        const {data: res} = await this.$http.delete(`goods/${row.goods_id}`);
        if(res.meta.status !== 200) return this.$message.error('删除商品失败！');
        this.$message.success('删除商品成功！');
        this.getGoodList();
      }
  },
  filters:{
      dateFormat:function(value){
          var data = new Date(value);
          var y = data.getFullYear();
          var m = (data.getMonth() + 1 + '').padStart(2, '0');
          var d = (data.getDay() + '').padStart(2, '0');
          var hh = (data.getHours() + '').padStart(2, '0');
          var mm = (data.getMinutes() + '').padStart(2, '0');
          var ss = (data.getSeconds() + '').padStart(2, '0');
          return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
      }
  }
};
</script>

<style lang="less" scoped>
.el-table{
    margin-top: 15px;
}
</style>